<template>
  <basic-container>
    <avue-crud
      ref="crud"
      v-model="form"
      :option="option"
      :table-loading="loading"
      :data="data"
      :page="page"
      :before-open="beforeOpen"
      :cell-style="cellStyle"
      :row-style="rowStyle"
      @row-update="rowUpdate"
      @row-save="rowSave"
      @row-del="rowDel"
      @row-click="rowClick"
      @refresh-change="refresh"
      @search-change="searchChange"
      @search-reset="searchReset"
      @selection-change="selectionChange"
      @current-change="currentChange"
      @size-change="sizeChange"
      @on-load="onLoad"
    >
      <template slot="menuLeft">
        <el-button
          type="danger"
          size="medium"
          icon="el-icon-delete"
          plain
          @click="handleDelete"
        >
          删 除
        </el-button>
      </template>
      <template slot="menu" slot-scope="scope">
        <el-button
          v-if="scope.row.notificationType===1"
          type="text"
          size="medium"
          icon="el-icon-edit"
          @click.stop="$refs.crud.rowEdit(scope.row,scope.$index)"
        >
          编辑
        </el-button>
      </template>
    </avue-crud>
  </basic-container>
</template>

<script>
import { mapGetters } from 'vuex'
import upload from '@/api/sys/upload'
const addSysNotificationAction = 'sys/notification/addSysNotification'
const updateSysNotificationAction = 'sys/notification/updateSysNotification'
const removeSysNotificationAction = 'sys/notification/removeSysNotification'
const removeSysNotificationsAction = 'sys/notification/removeSysNotifications'
const getSysNotificationByIdAction = 'sys/notification/getSysNotificationById'
const getSysNotificationPageListAction = 'sys/notification/pageSysNotification'

export default {
  name: 'SysNotification',
  data () {
    return {
      form: {},
      query: {},
      loading: true,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0
      },
      selectionList: [],
      option: {
        // title: '系统通知表(邮件,短信,微信公众号消息,微信小程序消息,app消息)',
        border: true,
        menu: true,
        lazy: true,
        tip: true,
        tipPlacement: 'top-start',
        index: true,
        indexLabel: '序号',
        indexFixed: true,
        multiple: false,
        selection: true,
        selectionWidth: 60,
        selectionFixed: false,
        searchShow: true,
        searchMenuSpan: 5,
        // header部分的左边设置
        header: true,
        addBtn: true,
        excelBtn: false,
        printBtn: false,
        viewBtn: true,
        editBtn: false,
        delBtn: true,
        // 弹出对话框设置
        dialogDrag: true,
        dialogDirection: 'rtl',
        dialogType: 'dialog',
        dialogWidth: '80%',
        // 表单设置
        enter: false,
        // 数据设置
        column: [
          {
            label: '通知类型',
            prop: 'notificationType',
            type: 'select',
            dicData: [
              { label: '站内信', value: 1 },
              { label: '邮件', value: 2 },
              { label: '短信', value: 3 },
              { label: '微信公众号', value: 4 },
              { label: '微信小程序消息', value: 5 },
              { label: 'APP消息', value: 6 }
            ],
            value: 1,
            hide: false,
            span: 24,
            width: 90,
            showWordLimit: true,
            maxlength: 200,
            showColumn: true,
            overHidden: true,
            addDisplay: true,
            editDisplay: true,
            viewDisplay: true,
            rules: [{
              required: true,
              message: '请选择通知类型',
              trigger: 'blur'
            }]
          },
          {
            label: '接收者',
            prop: 'userName',
            type: 'input',
            hide: false,
            span: 12,
            search: true,
            searchSpan: 10,
            width: 130,
            searchPlaceholder: '请输入需要查询通知对应的接收者',
            showWordLimit: true,
            maxlength: 200,
            showColumn: true,
            overHidden: true,
            addDisplay: false,
            editDisplay: false,
            viewDisplay: true,
            rules: [{
              required: true,
              message: '请输入',
              trigger: 'blur'
            }]
          },
          {
            label: '标题',
            prop: 'title',
            type: 'input',
            hide: false,
            span: 24,
            width: 200,
            showWordLimit: true,
            maxlength: 200,
            showColumn: true,
            overHidden: true,
            addDisplay: true,
            editDisplay: true,
            viewDisplay: true,
            rules: [{
              required: true,
              message: '请输入通知内容',
              trigger: 'blur'
            }]
          },
          {
            label: '通知内容',
            prop: 'content',
            component: 'editor',
            hide: false,
            span: 24,
            width: 600,
            showWordLimit: true,
            maxlength: 200,
            showColumn: true,
            overHidden: true,
            addDisplay: true,
            editDisplay: true,
            viewDisplay: true,
            rules: [{
              required: true,
              message: '请输入通知内容',
              trigger: 'blur'
            }],
            // quill-editor上传配置参数
            // modules: {
            //   toolbar: {
            //     container: [
            //       ['bold', 'italic', 'underline', 'strike'], // 加粗、倾斜、下划线、删除线
            //       [{ 'header': 1 }, { 'header': 2 }], // 标题一、标题二
            //       [{ 'list': 'ordered' }, { 'list': 'bullet' }], // 列表
            //       [{ 'color': [] }, { 'background': [] }], // 字体颜色、背景颜色
            //       [{ 'align': [] }, 'image']
            //     ]
            //   }
            // },
            options: {
              // 普通图片上传
              action: upload.UPLOAD_URL,
              props: {
                res: 'data',
                url: 'url'
              }
            }
          }
          // {
          //   label: '已读',
          //   prop: 'readStatus',
          //   type: 'radio',
          //   dicData: [
          //     { label: '已读', value: 1 }, { label: '未读', value: 2 }
          //   ],
          //   hide: false,
          //   span: 12,
          //   width: 90,
          //   showWordLimit: true,
          //   maxlength: 200,
          //   showColumn: true,
          //   overHidden: true,
          //   addDisplay: false,
          //   editDisplay: false,
          //   viewDisplay: true,
          //   rules: [{
          //     required: true,
          //     message: '请选择已读状态',
          //     trigger: 'blur'
          //   }]
          // }
        ]
      },
      data: []
    }
  },
  computed: {
    ...mapGetters(['permissions', 'website', 'userInfo']),
    ids () {
      const ids = []
      this.selectionList.forEach((e) => {
        ids.push(e.id)
      })
      return ids
    }
  },
  methods: {
    rowStyle ({ row, column, rowIndex }) {
      return {}
    },
    cellStyle ({ row, column, rowIndex, columnIndex }) {
      // return {
      //   color: 'green',
      //   fontWeight: 'bold',
      //   fontSize: '20'
      // }
      return {}
    },
    rowSave (row, loading, done) {
      loading()
      row.userId = this.userInfo.id
      this.$store.dispatch(addSysNotificationAction, row).then((res) => {
        done()
        const resCode = res.code
        const message = res.message
        if (resCode === 0) {
          this.onLoad(this.page)
          this.$message({
            type: 'success',
            message: '保存操作成功!'
          })
        } else {
          this.$message({
            type: 'error',
            showClose: true,
            message: message || '保存数据失败,请稍后再试!'
          })
        }
      }).catch(() => {
        done()
        this.$message({
          type: 'error',
          showClose: true,
          message: '保存数据失败,请稍后再试!'
        })
      })
    },
    rowUpdate (row, index, loading, done) {
      loading()
      row.userId = this.userInfo.id
      this.$store.dispatch(updateSysNotificationAction, row).then((res) => {
        done()
        const resCode = res.code
        const message = res.message
        if (resCode === 0) {
          this.onLoad(this.page)
          this.$message({
            type: 'success',
            message: '编辑操作成功!'
          })
        } else {
          this.$message({
            type: 'error',
            showClose: true,
            message: message || '编辑数据失败,请稍后再试!'
          })
        }
      }).catch(() => {
        done()
        this.$message({
          type: 'error',
          showClose: true,
          message: '编辑数据异常,请稍后再试!'
        })
      })
    },
    rowDel (row) {
      this.$confirm('确定将选择数据删除?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.loading = true
          const idParams = { id: row.id }
          return this.$store.dispatch(removeSysNotificationAction, idParams)
        })
        .then((res) => {
          this.loading = false
          const resCode = res.code
          const message = res.message
          if (resCode === 0) {
            this.onLoad(this.page)
            this.$message({
              type: 'success',
              message: '删除操作成功!'
            })
          } else {
            this.loading = false
            this.$message({
              type: 'error',
              showClose: true,
              message: message || '删除数据失败,请稍后再试!'
            })
          }
        }).catch(() => {
          this.loading = false
        })
    },
    handleDelete () {
      if (this.selectionList.length === 0) {
        this.$message.warning('请选择至少一条数据')
        return
      }
      this.$confirm('确定将选择数据删除?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.loading = true
          const idParams = { ids: this.ids }
          return this.$store.dispatch(removeSysNotificationsAction, idParams)
        })
        .then((res) => {
          this.loading = false
          const resCode = res.code
          const message = res.message
          if (resCode === 0) {
            this.$message({
              type: 'success',
              message: '批量删除操作成功!'
            })
            this.onLoad(this.page)
            this.$refs.crud.toggleSelection()
          } else {
            this.loading = false
            this.$message({
              type: 'error',
              showClose: true,
              message: message || '批量删除数据失败,请稍后再试!'
            })
          }
        }).catch(() => {
          this.loading = false
        })
    },
    rowClick (row, event, column) {
      this.$refs.crud.rowView(row, column)
    },
    refresh ({ page, searchForm }) {
      this.onLoad(page)
    },
    beforeOpen (done, type) {
      if (['edit', 'view'].includes(type)) {
        const idParams = { id: this.form.id }
        this.$store.dispatch(getSysNotificationByIdAction, idParams).then((res) => {
          const resCode = res.code
          const message = res.message
          if (resCode === 0) {
            this.form = res.data
          } else {
            this.$message({
              type: 'error',
              showClose: true,
              message: message || '查询数据失败,请稍后再试!'
            })
          }
        }).catch((err) => {
          console.log(err)
          this.$message({
            type: 'error',
            showClose: true,
            message: '查询数据异常,请稍后再试!'
          })
        })
      }
      done()
    },
    searchReset () {
      this.query = {}
      this.onLoad(this.page)
    },
    searchChange (params, done) {
      done()
      this.query = params
      this.page.currentPage = 1
      this.onLoad(this.page, params)
    },
    selectionChange (list) {
      this.selectionList = list
    },
    selectionClear () {
      this.selectionList = []
      this.$refs.crud.toggleSelection()
    },
    currentChange (currentPage) {
      this.page.currentPage = currentPage
    },
    sizeChange (pageSize) {
      this.page.pageSize = pageSize
    },
    onLoad (page, params = {}) {
      this.loading = true
      let queryData = {
        pageNo: page.currentPage,
        pageSize: page.pageSize
      }
      queryData = Object.assign(queryData, Object.assign(params, this.query))
      this.$store.dispatch(getSysNotificationPageListAction, queryData).then((res) => {
        this.loading = false
        const resCode = res.code
        const message = res.message
        if (resCode === 0) {
          const data = res.data
          this.page.total = parseInt(data.total)
          this.data = data.records
          this.selectionClear()
        } else {
          this.$message({
            type: 'error',
            showClose: true,
            message: message || '查询数据失败,请稍后再试!'
          })
        }
      }).catch((err) => {
        console.log(err)
        this.$message({
          type: 'error',
          showClose: true,
          message: '查询数据异常,请稍后再试!'
        })
      })
    }
  },
  head () {
    return {
      title: '公告通知管理 | ' + this.website.name,
      meta: [
        { hid: 'description', name: 'description', content: '公告通知管理' }
      ]
    }
  }
}
</script>

<style scoped>
</style>
